﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using CSWriter.Properties;
using System.Web.Services.Protocols;
using CommunityServerRest;
using CSWriter.Blogs;
using Microsoft.WindowsMobile.Forms;

namespace CSWriter {
	static class Program {
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[MTAThread]
		static void Main(string[] args) {
			if (args.Length == 0) {
				Application.Run(new Principal());
			} else if (args.Length==1) {
				OpenEditorWithAttachment(args[0]);
			}
		}

		private static void OpenEditorWithCapture() {
			Settings settings = Settings.Load();
			BlogService service = new BlogService(settings.ServiceCredentials);
			BlogManager manager = new BlogManager(service);

			CameraCaptureDialog capDlg = new CameraCaptureDialog();
			capDlg.Mode = CameraCaptureMode.Still;
			if (capDlg.ShowDialog() == DialogResult.OK) {

				//Crea un post personalizado
				Cursor.Current = Cursors.WaitCursor;
				BlogPostItem post = new BlogPostItem(Resources.CameraCaptureTitle, string.Format(Resources.CameraCaptureBody, DateTime.Now.Date), new Attachment(capDlg.FileName));
				Editor editor = new Editor(manager, post, settings, true);
				Cursor.Current = Cursors.Default;
				Application.Run(editor);
			}
		}

		private static void OpenEditorWithAttachment(string path) {
			try {
				Settings settings = Settings.Load();
				BlogService service = new BlogService(settings.ServiceCredentials);
				BlogManager manager = new BlogManager(service);

				//Crea un post personalizado
				BlogPostItem post = new BlogPostItem(Path.GetFileNameWithoutExtension(path), Resources.AutoAttach, new Attachment(path));
				Editor editor = new Editor(manager, post, settings, true);
				Application.Run(editor);
			} catch (IOException) {
				MessageBox.Show(Resources.FileCannotBeRead);
			} 
		}

	}
}